﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;

namespace COFE.Core
{
    public class EntryListRegistrar : IEntryListRegistrar
    {
        #region Constructor

        #endregion

        #region Methods

        public void RegisterEntryList(IEntryList entryList)
        {
            lock (_entryListDic)
            {
                if (_entryListDic.ContainsKey(entryList.Name))
                    _entryListDic[entryList.Name] = new Tuple<IEntryList, string>(entryList, COFEShared.CurrentUserProfile.UserName);
                else
                    _entryListDic.Add(entryList.Name, new Tuple<IEntryList, string>(entryList, COFEShared.CurrentUserProfile.UserName));
            }

        }


        public void RemoveEntryList(string entryListId)
        {
            lock (_entryListDic)
            {
                if (_entryListDic.ContainsKey(entryListId))
                {
                    if (_entryListDic[entryListId].Item2 != COFEShared.CurrentUserProfile.UserName)
                        throw new System.Security.SecurityException("Different user name.");
                    _entryListDic.Remove(entryListId);
                }
            }
        }

        public string[] GetEntryLists()
        {
            return (from ppair in _entryListDic where (ppair.Value.Item2 == COFEShared.CurrentUserProfile.UserName) select ppair.Key).ToArray();
        }

        public IEntryList this[string name]
        {
            get
            {
                return (from ppair in _entryListDic
                        where ppair.Key == name && (ppair.Value.Item2 == COFEShared.CurrentUserProfile.UserName)
                        select ppair.Value.Item1).FirstOrDefault();
            }
        }



        #endregion

        #region Data

        Dictionary<string, Tuple<IEntryList, string>> _entryListDic = new Dictionary<string, Tuple<IEntryList, string>>();

        #endregion

        #region Public Properties

        #endregion




    }
}
